//
//  TheNewAddressTableViewCell.m
//  BuyWhat
//
//  Created by ios on 2016/10/31.
//  Copyright © 2016年 吕金状. All rights reserved.
//

#import "TheNewAddressTableViewCell.h"

@interface TheNewAddressTableViewCell ()<UITextFieldDelegate>

@property (nonatomic , strong) UIImageView *titleImageView;
@property (nonatomic , strong) UITextField *textField;
@property (nonatomic , strong) UIView *lineView;

@end

@implementation TheNewAddressTableViewCell

- (id)initWithCellIdentifier:(NSString *)cellID{
    if (self = [super initWithCellIdentifier:cellID]) {
        self.selectionStyle = UITableViewCellStyleDefault;
        
        _titleImageView = [[UIImageView alloc]init];
        _titleImageView.contentMode  = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_titleImageView];
        
        _textField = [[UITextField alloc]init];
        _textField.font = DEF_FontSize_13;
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textField.delegate = self;
        [self.contentView addSubview:_textField];
        
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = getColor(dividerColor);
        [self.contentView addSubview:self.lineView];
        
        [self layoutFrame];
    }
    return self;
}

- (void)layoutFrame{
    __weak __typeof(self)weakSelf = self;
    [self.titleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf).offset(15 / WIDTH_5S_SCALE);
        make.width.mas_offset(25 / WIDTH_5S_SCALE);
        make.top.equalTo(weakSelf).offset(7 / WIDTH_5S_SCALE);
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_bottom).offset(0);
        make.left.equalTo(weakSelf).offset(15 / WIDTH_5S_SCALE);
        make.width.mas_offset(SCREEN_WIDTH - 15 / WIDTH_5S_SCALE);
        make.height.mas_offset(0.5);
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf).offset(0);
        make.bottom.equalTo(weakSelf).offset(0);
        make.left.equalTo(weakSelf.titleImageView.mas_right).offset(22 / WIDTH_5S_SCALE);
        make.right.equalTo(weakSelf).offset(- 20 / WIDTH_5S_SCALE);
    }];
}

- (void)setModel:(NSDictionary *)model{
    _model = model;
    self.lineView.hidden = NO;
    self.titleImageView.image = [UIImage imageNamed:model[@"image"]];
    self.textField.placeholder = model[@"title"];
        self.textField.text = model[@"text"];
    if ([model[@"title"] isEqualToString:@"请输入详细地址"]) {
        self.lineView.hidden = YES;
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (_getTextField) {
        _getTextField(textField.text);
    }
}
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//    NSLog(@"222%@",textField.text);
//    return YES;
//}


#pragma mark - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *str = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if ([textField.placeholder isEqualToString:@"请输入姓名"]) {
        if ([self unicodeLengthOfString:str] >= 10) {
            return NO;
        }
    }
    if ([textField.placeholder isEqualToString:@"请输入手机号码"]) {
        if ([self unicodeLengthOfString:str] >= 11) {
            return NO;
        }
    }
    if ([textField.placeholder isEqualToString:@"请输入详细地址"]) {
        if ([self unicodeLengthOfString:str] >= 100) {
            return NO;
        }
    }
    
    return YES;
}

-(NSUInteger) unicodeLengthOfString: (NSString *) text {
    NSUInteger asciiLength = 0;
    
    for (NSUInteger i = 0; i < text.length; i++) {
        
        
        unichar uc = [text characterAtIndex: i];
        
        asciiLength += isascii(uc) ? 1 : 2;
    }
    
    NSUInteger unicodeLength = asciiLength / 2;
    
    if(asciiLength % 2) {
        unicodeLength++;
    }
    
    return unicodeLength;
}


@end
